package com.example.springboot.filter;

import com.example.springboot.config.mysql.DataSourceContext;

import javax.servlet.*;
import java.io.IOException;

/**
 *
 * @Author:ChenZhangKun
 * @Date: 2020/11/12 23:04
 */
public class DataSourceFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        // 设置数据源
        DataSourceContext.setDataSource("slave");
        System.out.println("xxxxxxxxxxxxxxxxxxxxxxx");
        filterChain.doFilter(servletRequest,servletResponse);
        // 释放，防止高并发时内存溢出
        DataSourceContext.clearDataSource();
    }

    @Override
    public void destroy() {

    }
}
